﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.Utils.Extensions;
using XCI.Windows;
using XCI.Helper;
using XCI.Extensions;

namespace XCI.Setting.Controls
{
    public partial class SettingGridGroupSummary : BaseUserControl
    {
        private GridView _gridView;

        public SettingGridGroupSummary()
        {
            InitializeComponent();
        }

        public void Init(GridView gridView)
        {
            this._gridView = gridView;
            if (gridView == null) return;
            LoadData();
            SetStatus();
        }

        protected void LoadData()
        {
            listBoxControl1.Items.BeginUpdate();
            listBoxControl1.Items.Clear();

            for (var index = 0; index < _gridView.GroupSummary.Count; index++)
            {
                GridSummaryItem item = _gridView.GroupSummary[index];
                var name = $"汇总项 - 序号 {index + 1}";
                if (!string.IsNullOrWhiteSpace(item.FieldName))
                {
                    name = $"汇总项 - {item.FieldName} ({index + 1})";
                }
                listBoxControl1.Items.Add(name);
            }

            listBoxControl1.Items.EndUpdate();
        }

        protected List<GridSummaryItem> ToSummaryList()
        {
            var list = new List<GridSummaryItem>(_gridView.GroupSummary.Count);
            _gridView.GroupSummary.ForEach(p => list.Add(p));
            return list;
        }

        protected void ToSummaryCollection(List<GridSummaryItem> list)
        {
            _gridView.GroupSummary.BeginUpdate();
            _gridView.GroupSummary.Clear();
            list.ForEach(p => _gridView.GroupSummary.Add(p));
            _gridView.GroupSummary.EndUpdate();
        }

        private void SetStatus()
        {
            var index = listBoxControl1.SelectedIndex;
            btnDelete.Enabled = index > -1;
            btnUp.Enabled = index > 0;
            btnDown.Enabled = index >= 0 && index < _gridView.GroupSummary.Count - 1;
        }

        private void MoveItem(int index, bool isUp)
        {
            var list = ToSummaryList();
            if (isUp)
            {
                list.MoveUp(index);
            }
            else
            {
                list.MoveDown(index);
            }
            ToSummaryCollection(list);
            LoadData();
            listBoxControl1.SelectedIndex = isUp ? (index - 1) : (index + 1);
        }

        private void listBoxControl1_SelectedValueChanged(object sender, EventArgs e)
        {
            SetStatus();
            var index = listBoxControl1.SelectedIndex;
            if (index == -1) return;

            propertyGridPlus.SelectedObject = _gridView.GroupSummary[index];
        }

        private void xciPropertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Label == null || !e.ChangedItem.Label.Equals("FieldName")) return;
            LoadData();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var item = new GridGroupSummaryItem(DevExpress.Data.SummaryItemType.None, string.Empty, null, string.Empty);
            _gridView.GroupSummary.Add(item);
            LoadData();
            listBoxControl1.SelectedIndex = _gridView.GroupSummary.Count - 1;
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中项吗？") == DialogResult.Yes)
            {
                _gridView.GroupSummary.RemoveAt(listBoxControl1.SelectedIndex);
                LoadData();
            }
        }

        private void btnUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MoveItem(listBoxControl1.SelectedIndex, true);
        }

        private void btnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MoveItem(listBoxControl1.SelectedIndex, false);
        }

        private void btnAuto_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowMessageYesNo("确定自动生成吗？") != DialogResult.Yes) return;

            var cols = _gridView.Columns;
            _gridView.GroupSummary.BeginUpdate();
            _gridView.GroupSummary.Clear();
            foreach (GridColumn col in cols)
            {
                var item = new GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Count, col.FieldName, col, string.Empty);
                _gridView.GroupSummary.Add(item);
            }
            _gridView.GroupSummary.EndUpdate();
            LoadData();
            listBoxControl1.SelectedIndex = 0;
        }

    }
}
